package com.hu.zhcc.shiro.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hu.zhcc.shiro.dao.MedicalExaminationReportDAO;
import com.hu.zhcc.shiro.entity.bo.MedicalExaminationReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("medicalExaminationReport")
public class MERServiceImpl implements MERService{
    @Autowired
    MedicalExaminationReportDAO merDAO;
    @Override
    public PageInfo<MedicalExaminationReport> getMedicalExaminationReportByName(Integer offset, Integer limit, String name) {
        PageHelper.startPage(offset,limit);
        List<MedicalExaminationReport> medicalExaminationReports = merDAO.listAllMedicalExaminationReport(name);
        PageInfo<MedicalExaminationReport> pageInfo = new PageInfo<>(medicalExaminationReports);

        return pageInfo;
    }

    @Override
    public PageInfo<MedicalExaminationReport> listAllMedicalExaminationReport(Integer offset, Integer limit) {
        return null;
    }

    @Override
    public MedicalExaminationReport getMedicalExaminationReport(Integer id) {
        MedicalExaminationReport mer = merDAO.selectByPrimaryKey(id);

        return mer;
    }

    @Override
    public int updateMedicalExaminationReport(MedicalExaminationReport mer) {
        if(mer.getReport()==null||mer.getReport().equals("")){
            mer.setStatus("报告未上传");
        }else {
            mer.setStatus("报告已上传");
        }
        int i = merDAO.updateByPrimaryKeySelective(mer);
        return i;
    }

    @Override
    public int addMedicalExaminationReport(MedicalExaminationReport mer) {
        if(mer.getReport()==null||mer.getReport().equals("")){
            mer.setStatus("报告未上传");
        }else {
            mer.setStatus("报告已上传");
        }
        int i = merDAO.insertSelective(mer);
        return i;
    }

    @Override
    public List<MedicalExaminationReport> findAllMedicalExaminationReportByFileNumber(String fileNumber) {
        List<MedicalExaminationReport> medicalExaminationReports = merDAO.listAllMedicalExaminationReport(null);
        List<MedicalExaminationReport> list = new ArrayList<>();
        for (MedicalExaminationReport medicalExaminationReport : medicalExaminationReports) {
            if (medicalExaminationReport.getFileNumber().equals(fileNumber)) {
                list.add(medicalExaminationReport);
            }
        }

        return list;
    }

    @Override
    public List<MedicalExaminationReport> findAllMedicalExaminationReportByPhoneNumber(String phone) {
        return merDAO.findAllMedicalExaminationReportByPhoneNumber(phone);
    }

    @Override
    public int deleteMedicalExaminationReport(Integer id) {
        int i = merDAO.deleteByPrimaryKey(id);
        return i;
    }
}
